<!--调试科研创新管理-年度预算-->
<template>
  <PageTemplate>
    <div class="plan-list" slot="content">
      <el-form
        inline
        :model="query"
        ref="query"
        label-position="right"
        label-width="110px"
        class="search-collapse-pro-form"
      >
        <SearchCollapsePro :isAdvSearch="false">
          <div slot="title" class="search-collapse-pro-title">
            <el-form-item label-width="0px">
              <el-input v-model="query.year" placeholder="预算年度">
                <el-button slot="append" icon="el-icon-search" @click="queryDataTableLike"></el-button>
              </el-input>
            </el-form-item>
          </div>
        </SearchCollapsePro>
      </el-form>
      <el-row class="row-empty-20"></el-row>
      <PageTableCollapse :collapseStatus="true">
        <el-row slot="title">
          <el-col>
            <div>
               <el-button v-if="showBtn" type="primary" icon="el-icon-edit" @click="addHandle">新增</el-button>
              <!-- <el-button type="primary" @click="editHandle">编辑</el-button> -->
              <el-button icon="el-icon-download" type="primary" @click="exportHandle">
                 导出
              </el-button>
            </div>
          </el-col>
        </el-row>
        <el-table
          class="table"
          border
          ref="multipleTable"
          :data="tables"
          tooltip-effect="dark"
          style="width: 100%"
          :show-overflow-tooltip="true"
          @selection-change="handleSelectionChange"
          :header-cell-style="{background:tableHeaderBGC}" >
          <el-table-column align="center" type="selection" width="60"></el-table-column>
          <el-table-column type="index" :index="indexMethod" :label="$t('cm.no')" align="center" width="60" fixed></el-table-column>
          <el-table-column prop="year" label="预算年度" align="center"></el-table-column>
          <el-table-column prop="travelExpense" label="差旅费" align="center"></el-table-column>
          <el-table-column prop="meetingExpense" label="会议费" align="center"></el-table-column>
          <el-table-column prop="consultingExpense" label="咨询费" align="center"></el-table-column>
          <el-table-column prop="businessSupportExpense" label="业务支持费" align="center"></el-table-column>
          <el-table-column prop="deviceExpense" label="设备费" align="center"></el-table-column>
          <el-table-column prop="softwareExpense" label="软件费" align="center"></el-table-column>
          <el-table-column prop="totalExpense" label="预算总额" align="center"></el-table-column>
          <el-table-column :label="$t('cm.operate')" prop="statussss" fixed="right" align="center">
            <template slot-scope="scope">
              <ListBtn 
                  :options="btns" 
                  :row-data="scope.row" 
                  @triggerListBtn="handleListBtn"
                  :show-btn="showBtn">
              </ListBtn>
            </template>
          </el-table-column>
        </el-table>
        <div class="block pager-customer">
          <el-pagination
            background
            layout="total,prev,pager, next,jumper"
            :current-page.sync="pagingCurrentPageNum"
            :page-sizes="pagingPageSizes"
            :page-size="pagingPageSize"
            :total="pagingTotal"
            @size-change="pagingHandleSizeChange"
            @current-change="pagingHandleCurrentChange"
          >
          </el-pagination>
        </div>
      </PageTableCollapse>
    </div>
  </PageTemplate>
</template>

<script>
import { mapState, mapActions } from 'vuex'
import PageTemplate from '@/components/common/PageTemplate'
import PageTableCollapse from '@/components/common/PageTableCollapse'
import SearchCollapsePro from '@/components/common/SearchCollapsePro'
import ListBtn from '@/components/btn/ListBtn'
import { ALERT } from '@/utils/responseHandle'
import * as Constant from '@/utils/constant'
import {COMMON} from '@/utils/commonUtil'
import Pagination from '@/mixins/Pagination'
import HasBtn from '@/mixins/HasBtn'
import api_sri from '@/api/api_sri'
import sys_manage from '@/api/sys_manage'
import {dateFtt} from '@/utils/datetime'

export default {
  components: { 
    PageTemplate, 
    PageTableCollapse, 
    SearchCollapsePro, 
    ListBtn 
  },
  data () {
    return {
      query: {
        year: null,
      },
      tables:[],
      btns: [
        {
          value: 'look',
          label: 'cm.look'
        },
        {
          value: 'edit',
          label: 'cm.edit'
        }
      ],
      indexTypeMap:[],//索引类别字典
      alpIdMap:[],//实验室类别字典

    }
  },
  created () {
    //获取全部字典
    this.getAllMap();
    this.getAllDataTable();
  },
  mixins: [Pagination, HasBtn],
  computed: {
    ...mapState({
      tableHeaderBGC: state => state.common.tableHeaderBGC
    })
  },
  methods:{
     //获取全部字典
    getAllMap(){
       //索引类别字典
      this.sysDictLoadByPaging("achievement_index_type",(result)=>{
          this.indexTypeMap = result.data
      })
    },
    //获取字典接口
    sysDictLoadByPaging (key,callback) { 
      sys_manage.sysDictLoadByPaging({dictGroupKey: key}, result => {
        if (result.code === Constant.RT_SUCCESS) {
          callback(result)
        } else {
          ALERT.error(result.msg)
        }
      })
    },
    handleListBtn({btn,row}){
      if(btn==="look"){//查看
        COMMON.routerTo(Constant.ROUTE_SRI_BUDGET_DETAILS.index, this.$router, { id: row.id})
      }else if(btn==="edit"){//编辑
        COMMON.routerTo(Constant.ROUTE_SRI_BUDGET_EDIT.index, this.$router, { id: row.id})
      }
    },
    //重置操作
    queryReset (formName) {
      this.$refs[formName].resetFields()
    },
    //新增
    addHandle(){
        COMMON.routerTo(Constant.ROUTE_SRI_BUDGET_ADD.index, this.$router)
    },
    // 编辑
    // editHandle () {
    //   COMMON.routerTo(Constant.ROUTE_SRI_BUDGET_EDIT.index, this.$router, { id: '2020' })
    // },
                //导出excel之前,勾选id
    handleSelectionChange(val) {
      this.multipleSelection = val
      this.query.ids = [];
      val.forEach(item => {
        this.query.ids.push(item.id);
      });
    },
    // 导出
    exportHandle () {
       let params = Object.assign({},this.query,{
        pageNum:this.pagingCurrentPageNum,
        pageSize:this.pagingPageSize
      })
      let fileName = `调试科研创新管理_预算管理_预算年度总和_${dateFtt('yyyy-MM-dd', new Date())}`
      api_sri.sriGainBudExportExcel(params, response => {
        if (response.code === Constant.RT_SUCCESS) {
          COMMON.downloadLinkByUrl(response.data.webRequestUrl, fileName)
        } else {
          ALERT.alertResp(response)
        }
      })
    },
    queryDataTableLike () {
      this.getAllDataTable();
    },
    getAllDataTable () {
      this.queryTableList({
        ...this.query,
        pageNum: this.pagingCurrentPageNum,
        pageSize: this.pagingPageSize
      })
    },
    //统一与后台操作的方法
    queryTableList (params) {
      //这里的params是对象
      //注意 如果后台json转对象 时间类型好像会抛异常  转到后台 前台用 qs 来转换数据 设置一个全局拦截器 main.js 参考
      let _this = this
      let tables = []
      let totalCount = 0
      api_sri.sriGainBudgetList(params, result => {
        if (result.code === Constant.RT_SUCCESS) {
          //表格数据  数组            
          tables = result.data.results
          //分页数据
          totalCount = result.data.totalRows
        } else {
          // _this.$t('cm.tips')
          ALERT.error(result.data.msg)
        }
        _this.setPagingTotal(totalCount)
        this.tables = tables
      })
    },
  }
}
  
</script>

<style lang="scss">
  
</style>